package com.cathyjava.easyflows.workflow.parallel;

import com.cathyjava.easyflows.WorkStatus;
import com.cathyjava.easyflows.work.WorkContext;
import com.cathyjava.easyflows.work.WorkReport;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author chenjing
 * @date 2021/5/12
 */
public class ParallelFlowReport implements WorkReport {
    private List<WorkReport> reports;

    public ParallelFlowReport() {
        this(new ArrayList<>());
    }

    public ParallelFlowReport(List<WorkReport> reports) {
        this.reports = reports;
    }

    @Override
    public WorkStatus getStatus() {
        for (WorkReport report : reports) {
            if (report.getStatus().equals(WorkStatus.FAIL)) {
                return WorkStatus.FAIL;
            }
        }
        return WorkStatus.COMPLETED;
    }

    @Override
    public WorkContext getWorkContext() {
        WorkContext workContext = new WorkContext();
        for (WorkReport report : this.reports) {
            for (Map.Entry<String, Object> entry : report.getWorkContext().getEntrySet()) {
                workContext.put(entry.getKey(), entry.getValue());
            }
        }
        return workContext;
    }

    public void addAll(List<WorkReport> workReports) {
        this.reports.addAll(workReports);
    }
}
